package com.yuan.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yuan.reggie.common.CustomerException;
import com.yuan.reggie.dto.DishDto;
import com.yuan.reggie.mapper.DishMapper;
import com.yuan.reggie.pojo.Dish;
import com.yuan.reggie.pojo.DishFlavor;
import com.yuan.reggie.service.DishFlavorService;
import com.yuan.reggie.service.DishService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

/**
 * @author yuanxingke
 * @date 2022/7/30 0:03
 */
@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {

    @Autowired
    private DishFlavorService dishFlavorService;


    @Override
    @Transactional
    public void saveWithFlavor(DishDto dishDto) {
        //1、保存菜品信息
        this.save(dishDto);

        //2、保存菜品的口味信息
        //取出dishDto中的美味信息
        List<DishFlavor> flavors = dishDto.getFlavors();

        //设置flavors中的每一个美味的菜品id
        flavors.forEach(flavor -> {
            flavor.setDishId(dishDto.getId());
        });

        dishFlavorService.saveBatch(flavors);
    }

    @Override
    public DishDto getByIdWithFlavor(Long id) {
        //根据id查询
        Dish dish = this.getById(id);

        //一对多，查询这个菜的美味条数
        LambdaQueryWrapper<DishFlavor> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(DishFlavor::getDishId, id);
        List<DishFlavor> list = dishFlavorService.list(lambdaQueryWrapper);

        //组合
        DishDto dishDto = new DishDto();
        dishDto.setFlavors(list);
        BeanUtils.copyProperties(dish, dishDto);
        return dishDto;
    }

    @Override
    @Transactional
    public void updateWithFlavor(DishDto dishDto) {

        //先更新菜表
        this.updateById(dishDto);

        //先删除后添加
        LambdaQueryWrapper<DishFlavor> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(DishFlavor::getDishId, dishDto.getId());
        dishFlavorService.remove(lambdaQueryWrapper);

        //添加美味表

        //需要设置dishid
        List<DishFlavor> flavors = dishDto.getFlavors();
        flavors.forEach(dishFlavor -> dishFlavor.setDishId(dishDto.getId()));

        dishFlavorService.saveBatch(flavors);

    }

    @Override
    public void removeWithFlavor(Long[] ids) {
        //先查询状态再删除

        LambdaQueryWrapper<Dish> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.in(Dish::getId,ids);
        lambdaQueryWrapper.eq(Dish::getStatus,1);

        int count = this.count(lambdaQueryWrapper);
        if(count>0){
            throw new CustomerException("菜品正在售卖，无法删除");
        }

        //删除菜品
        this.removeByIds(Arrays.asList(ids));

        //删除菜品对应的口味
        LambdaQueryWrapper<DishFlavor> lambdaQueryWrapper1=new LambdaQueryWrapper<>();
        lambdaQueryWrapper1.in(DishFlavor::getDishId,ids);
        dishFlavorService.remove(lambdaQueryWrapper1);


    }
}
